package com.ts.api.http.demo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.IteratorUtils;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiBizStatus;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.platform.entity.RespPlatformMultiPhoneMaskDataExchange;
import com.ts.api.module.api.convert.platform.entity.RespPlatformNoticeApply;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMaskApply;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMaskDataExchange;
import com.ts.api.module.api.convert.platform.handler.ApiPlatformPhoneMaskConvertHandler;
import com.ts.api.module.api.entity.ApiProtocol;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 *  全流程手机号掩码
 *
 *  @author Pan
 */
@ApiPlan(productId = "1000065", type = ApiPlanConvertType.PLAT_PHONE_MASK)
public class DemoApiPlatV3TestPhoneMaskConvert implements ApiPlatformPhoneMaskConvertHandler {

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        return JsonUtils.toJson(MapUtils.newHashMap(newMap -> {
            newMap.put("phoneMask", apiDataExchangeParameter.getPhoneMask());
        }));
    }

    @Override
    public RespPlatformMultiPhoneMaskDataExchange respDataExchange(String respJson) throws Exception {
        //  状态验证
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespPlatformMultiPhoneMaskDataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 1000)) {
            return RespPlatformMultiPhoneMaskDataExchange.ofFail();
        }

        //  验证返回data是否存在
        JSONObject resData = JsonUtils.get(respJson);
        JSONArray data = resData.getJSONArray("data");
        if (ValidParam.isEmpty(data)) {
            return RespPlatformMultiPhoneMaskDataExchange.ofFail();
        }

        //  解析属性
        RespPlatformMultiPhoneMaskDataExchange respPlatformMultiPhoneMaskDataExchange = RespPlatformMultiPhoneMaskDataExchange.ofSuc();
        List<RespPlatformPhoneMaskDataExchange> listRespPlatformPhoneMaskDataExchange = CollUtils.newArrayList();

        for (int i = 0; i < data.size(); i++) {
            JSONObject item = data.getJSONObject(i);
            if (ValidParam.isNotNull(item)) {
                RespPlatformPhoneMaskDataExchange respPlatformPhoneMaskDataExchange = RespPlatformPhoneMaskDataExchange.of();

                BigDecimal price = item.getBigDecimal("price");
                String logoPath = item.getString("logoPath");
                String companyName = item.getString("companyName");
                String productName = item.getString("productName");
                String applyId = item.getString("applyId");

                //  添加协议
                List<ApiProtocol> listProtocol = CollUtils.newArrayList(newList -> {
                    //  获取协议
                    JSONArray listProtocolResult = item.getJSONArray("protocolList");
                    IteratorUtils.array(listProtocolResult, (protocolResult, index) -> {
                        JSONObject protocolObject = listProtocolResult.getJSONObject(index);
                        ApiProtocol protocol = new ApiProtocol();
                        protocol.setProtocolName(protocolObject.getString("protocolName"));
                        protocol.setProtocolUrl(protocolObject.getString("protocolUrl"));
                        newList.add(protocol);
                    });
                });

                //  获取手机号掩码
                List<String> listPhoneMd5 = CollUtils.newArrayList(newList -> {
                    JSONArray jsonArray = item.getJSONArray("listPhoneMd5");
                    IteratorUtils.array(jsonArray, (protocolResult, index) -> {
                        String phoneMd5  = jsonArray.getString(index);
                        newList.add(phoneMd5);
                    });
                });

                respPlatformPhoneMaskDataExchange.setApplyId(applyId);
                respPlatformPhoneMaskDataExchange.setProductName(productName);
                respPlatformPhoneMaskDataExchange.setDisplayName(companyName);
                respPlatformPhoneMaskDataExchange.setLogoPath(logoPath);
                respPlatformPhoneMaskDataExchange.setPrice(price);
                respPlatformPhoneMaskDataExchange.setProtocolList(listProtocol);
                respPlatformPhoneMaskDataExchange.setListPhoneMd5(listPhoneMd5);
                listRespPlatformPhoneMaskDataExchange.add(respPlatformPhoneMaskDataExchange);
            }
        }

        respPlatformMultiPhoneMaskDataExchange.setListPhoneMask(listRespPlatformPhoneMaskDataExchange);
        return respPlatformMultiPhoneMaskDataExchange;
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        return JsonUtils.toJson(apiApplyParameter);
    }

    @Override
    public RespPlatformPhoneMaskApply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespPlatformPhoneMaskApply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 1000)) {
            return RespPlatformPhoneMaskApply.ofFail();
        }
        return RespPlatformPhoneMaskApply.ofSuc();
    }

    @Override
    public RespPlatformNoticeApply parseApplyNotice(RequestVO requestVO) {
        return null;
    }

    @Override
    public Map<String, Object> respApplyNotice(ApiBizStatus apiBizStatus) {
        return Map.of();
    }
}
